<template>
  <div class="app-main">
    <div class="main-header">
      <el-page-header @back="reset('ruleForm')" content="新增备忘录" />
      <div class="header-right">
        <el-button size="mini" @click="reset('ruleForm')">取 消</el-button>
        <el-button size="mini" type="primary" :loading="loading" @click="save('ruleForm')" >确 定</el-button>
      </div>
    </div>
    <div class="main-contain">
      <el-form :model="ruleForm" label-position="top" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <div class="contain-item">
          <el-form-item label="备忘录名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="重要性" prop="importance">
            <el-rate v-model="ruleForm.importance" show-text :texts="texts"></el-rate>
          </el-form-item>
        </div>
        <div class="contain-item">
          <el-form-item label="备忘录标签">
            <div class="form-item-tag">
              <d-tag :dynamicTags.sync="dynamicTags" />
            </div>
          </el-form-item>
        </div>
      </el-form>
      <div class="contain-editor">
        <div class="editor-nav">
          <span>备忘录内容</span>
          <el-button type="primary" plain size="mini" @click="changeEditor">切换</el-button>
        </div>
        <template v-if="cureditor === 'mavon'">
          <dmavon-editor :content.sync="ruleForm.content" />
        </template>
        <template v-else>
          <dquill-editor :content.sync="ruleForm.content"/>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import dmavonEditor from '@/components/mavonEditor'
import dquillEditor from '@/components/quillEditor'
export default {
  name: 'addMemery',
  components: {
    dmavonEditor,
    dquillEditor
  },
  data () {
    return {
      texts: ['不重要', '合适', '一般', '重要', '非常重要'],
      loading: false,
      ruleForm: {
        name: '',
        importance: 0,
        content: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入备忘录名称', trigger: 'blur' }
        ]
      },
      dynamicTags: [],
      cureditor: 'mavon'
    }
  },
  methods: {
    changeEditor () {
      if (this.cureditor === 'mavon') {
        this.cureditor = 'quill'
      } else {
        this.cureditor = 'mavon'
      }
    },
    reset(formName) {
      this.$refs[formName].resetFields()
      this.dynamicTags = []
      this.$router.go(-1)
    },
    save(formName) { // 新增
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            name: this.ruleForm.name,
            importance: this.ruleForm.importance,
            content: this.ruleForm.content,
            tag: this.dynamicTags.join(','),
            uid: this.$Cookies.get('uid')
          }
          this.$API({
            name: 'AddMemery',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200) {
              this.$router.go(-1)
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-form-item {
  margin-bottom: 15px !important;
}
.app-main {
  .main-header {
    height: 31px;
    width: 100%;
    border-bottom: 1px solid #DCDFE6;
    flex-shrink: 0;
    display: flex;
    justify-content: space-between;
  }
  .main-contain {
    padding-top: 10px;
    overflow-y: auto;
    width: 100%;
    flex: 1 1 auto;
    .contain-item {
      display: flex;
      .el-form-item {
        padding: 0 5px;
        flex: 1;
      }
      .el-rate {
        border-radius: 4px;
        border: 1px solid #DCDFE6;
        padding: 0 15px
      }
      .form-item-tag {
        border-radius: 4px;
        border: 1px solid #DCDFE6;
        padding: 0 15px;
      }
    }
    .el-rate {
      height: 40px !important;
      line-height: 2.5 !important;
    }
    .contain-editor {
      width: 100%;
      height: 500px;
      display: flex;
      flex-direction: column;
      .editor-nav {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 10px;
        padding: 0 5px;
        span {
          color: #606266;
          font-size: 14px;
        }
      }
      .editor {
        flex: 1 1 auto;
      }
    }
  }
}
</style>